package com.example.cloud_300.service.Impl;

import com.example.cloud_300.service.ImportService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ImportServiceImpl implements ImportService {
    @Override
    public void importExcel() {
        try {
            String filePath = "车型名称映射.xlsx";
            List<String> aModels = readExcel(filePath, "A");
            List<String> bModels = readExcel(filePath, "B");
            List<String[]> mappings = mapCarModels(aModels, bModels);
            writeExcel("匹配结果.xlsx", mappings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void writeExcel(String filePath, List<String[]> results) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("匹配结果");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("A表车型");
        headerRow.createCell(1).setCellValue("B表车型");

        // 写入数据
        int rowCount = 1;
        for (String[] result : results) {
            Row row = sheet.createRow(rowCount++);
            row.createCell(0).setCellValue(result[0]); // A表车型
            row.createCell(1).setCellValue(result[1]); // B表车型
        }

        // 写入文件
        try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
            workbook.write(fos);
            System.out.println("结果已成功写入至 " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String[]> mapCarModels(List<String> carModelsA, List<String> carModelsB) {
        List<String[]> mapping = new ArrayList<>();
        for (String modelA : carModelsA) {
            for (String modelB : carModelsB) {
                if (isMatch(modelA, modelB)) {
                    mapping.add(new String[]{modelA, modelB});
                    break; // 找到一个匹配就跳出
                }
            }
        }
        return mapping;
    }

    private static boolean isMatch(String modelA, String modelB) {
        String standardizedA = standardizeCarName(modelA);
        String standardizedB = standardizeCarName(modelB);
        return standardizedA.equals(standardizedB);
    }

    private static String standardizeCarName(String model) {
        List<String> list = Arrays.asList(model.split(" "));
        return list.get(0) + list.get(1) + list.get(2) + list.get(list.size() - 1);
    }

    private static List<String> readExcel(String filePath, String sheetName) {
        List<String> models = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheet(sheetName);
            if (sheet != null) {
                // 从第二行开始读取（索引为1）
                for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                    Row row = sheet.getRow(rowIndex);
                    if (row != null) {
                        Cell cell = row.getCell(0);
                        if (cell != null) {
                            models.add(cell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return models;
    }
}
